package com.ziyun.erp.modules.data.service.impl;

import com.utility.StringUtils;
import com.ziyun.erp.common.e.order.*;
import com.ziyun.erp.modules.data.entity.OtherAccessoryDataEntity;
import com.ziyun.erp.modules.data.entity.SellerInfoDataCopyEntity;
import com.ziyun.erp.modules.data.entity.SellerInfoDataEntity;
import com.ziyun.erp.modules.data.service.*;
import com.ziyun.erp.modules.data.vo.OrderInfoVo;
import com.ziyun.erp.modules.data.vo.SellerImageUrlVo;
import com.ziyun.erp.modules.order.entity.ErpOrderEntity;
import com.ziyun.erp.modules.order.service.ErpOrderService;
import com.ziyun.erp.modules.production.entity.ErpOrderItemEntity;
import com.ziyun.erp.modules.production.service.ErpOrderItemService;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author gaosheng
 * @date 2018-10-11 19:06
 */
@Service("dataTransferService")
public class DataTransferServiceImpl implements DataTransferService{

    @Autowired
    private SdbZyywMerchantsService sdbZyywMerchantsService;
    @Autowired
    private ErpSupplierService erpSupplierService;
    @Autowired
    private OtherAccessoryDataService otherAccessoryDataService;
    @Autowired
    private OrderInfoExtendService orderInfoExtendService;
    @Autowired
    private ErpOrderService erpOrderService;
    @Autowired
    private ErpOrderItemService erpOrderItemService;
    @Autowired
    private SellerInfoDataService sellerInfoDataService;
    @Autowired
    private SellerInfoDataCopyService sellerInfoDataCopyService;
    @Autowired
    private OrderItemExtendService orderItemExtendService;

    @Override
    public R sellerInfoDataTransfer() {
        long begin = System.currentTimeMillis();
        List<SellerInfoDataEntity> merchantList = sdbZyywMerchantsService.selectMerchantList();
        sellerInfoDataService.insertBatch(merchantList, 20000);
        long end = System.currentTimeMillis();
        long time = (begin-end)/1000/60;
        return R.ok().put("time", time);
    }

    @Override
    public R factoryDataTransfer() {
        long begin = System.currentTimeMillis();
        /* 状态待申请状态置为待审核，已审核但是没有报价账号的工厂也置为待审核状态 */
        List<SellerInfoDataCopyEntity> factoryList = erpSupplierService.selectFactoryList();
        sellerInfoDataCopyService.insertBatch(factoryList, 1000);
        long end = System.currentTimeMillis();
        long time = (begin-end)/1000/60;
        return R.ok().put("time", time);
    }

    @Override
    public R sellerImageDataTransfer(String type) {
        long begin = System.currentTimeMillis();
        SellerImageUrlVo sellerImageVo = sdbZyywMerchantsService.selectImageUrlList();
        List<String> businessUrlList = Arrays.asList(sellerImageVo.getBusinessUrl().split(","));
        List<String> identificationUrlList = Arrays.asList(sellerImageVo.getIdentificationUrl().split(","));
        List<String> logoUrlList = Arrays.asList(sellerImageVo.getLogoUrl().split(","));
        List<String> getSellerImageUrlList = Arrays.asList(sellerImageVo.getSellerImageUrl().split(","));
        List<String> dataList = new ArrayList<>();
        if (!StringUtils.isBlank(type)){
            if ("business".equals(type)){
                dataList.addAll(businessUrlList);
            }else if ("ident".equals(type)){
                dataList.addAll(identificationUrlList);
            }else if ("log".equals(type)){
                dataList.addAll(logoUrlList);
            }else if ("image".equals(type)){
                dataList.addAll(getSellerImageUrlList);
            }
        }else {
            dataList.addAll(businessUrlList);
            dataList.addAll(identificationUrlList);
            dataList.addAll(logoUrlList);
            dataList.addAll(getSellerImageUrlList);
        }

        List<String> filterList = dataList.stream()
                .filter(b -> StringUtils.isNotBlank(b) && !StringUtils.startsWith(b, "upload"))
                .collect(Collectors.toList());
        Set<String> dataSet = new HashSet<>(filterList);
        String[] idArr = dataSet.toArray(new String[dataSet.size()]);
        List<OtherAccessoryDataEntity> otherAccessoryList =  sdbZyywMerchantsService.selectImageInfoList(idArr);
        new Thread(() -> otherAccessoryDataService.insertBatch(otherAccessoryList, 20000)).start();
        long end = System.currentTimeMillis();
        long time = (end-begin)/1000/60;
        return R.ok().put("time", time);
    }

    @Override
    public R orderInfoDataTransfer(String beginTime, String endTime) {
        long begin = System.currentTimeMillis();
        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(beginTime)){
            param.put("beginTime", beginTime);
        }
        if (StringUtils.isNotBlank(endTime)){
            param.put("endTime", endTime);
        }
        List<ErpOrderEntity> erpOrderlist = new ArrayList<>();
        List<OrderInfoVo> orderInfoVoList = orderInfoExtendService.selectOrderInfoList(param);
        for (OrderInfoVo orderInfoEntity : orderInfoVoList){
            ErpOrderEntity erpOrderEntity = new ErpOrderEntity();
            erpOrderEntity.setOrderNo(orderInfoEntity.getOrderId());
            erpOrderEntity.setMemberId(orderInfoEntity.getMemberId());
            erpOrderEntity.setMemberName(orderInfoEntity.getMemberName());
            if ("erp".equals(orderInfoEntity.getSource())){
                erpOrderEntity.setOrderSource(OrderSourceEnum.OFFlINE.toInt());
            }else {
                erpOrderEntity.setOrderSource(OrderSourceEnum.ZIYUN.toInt());
            }
            if (orderInfoEntity.getSupplierId() == null){ // 工厂订单
                erpOrderEntity.setThirdMerchantType(ThirdMerchantTypeEnum.FACTOEY.toInt());
                erpOrderEntity.setThirdMerchantId(orderInfoEntity.getFactoryId());
            } else {
                if(orderInfoEntity.getSupplierId().intValue() == 1){ // 紫云自营
                    erpOrderEntity.setThirdMerchantType(ThirdMerchantTypeEnum.ZIYUN.toInt());
                } else {
                    if(orderInfoEntity.getIsSupplier().intValue() == 1){
                        erpOrderEntity.setThirdMerchantType(ThirdMerchantTypeEnum.ZIYUN_MERCHANT.toInt());
                    } else {
                        erpOrderEntity.setThirdMerchantType(ThirdMerchantTypeEnum.MERCHANT.toInt());
                    }
                }
                erpOrderEntity.setThirdMerchantId(orderInfoEntity.getSupplierId());
            }
            erpOrderEntity.setOriginalOrderId(Long.valueOf(orderInfoEntity.getOrderId()));
            // 付款状态:0:未支付,1:已支付,2:已付款至到担保方,3:部分付款,4:部分退款,5:全额退款,6:待审核订单,7:支付待审核,8:已支付待审核(转账),9:挂账(门店）
            String payStatus = orderInfoEntity.getPayStatus();
            // 原审核状态 0：未审核 1：已审核
            Integer orderApprovalStatus = orderInfoEntity.getOrderApprovalStatus();

            if("finish".equals(orderInfoEntity.getStatus())){
                erpOrderEntity.setProductionStatus(ProductionStatusEnum.FINISH.toInt());
            }else if("dead".equals(orderInfoEntity.getStatus())){
                erpOrderEntity.setProductionStatus(ProductionStatusEnum.CANCELLED.toInt());
            }else{
                // 现订单生产状态  0：待分配，1.已分配，2.生产中，3.生产完成
                // 现 erp_order生产状态  1.待审核;2.待分配;3.生产中;4.生产完成;5.已发货;6.已完成;7.新开单(门店);11.已作废
                if (StringUtils.equals(orderInfoEntity.getSource(), "erp")){
                    // 原order_info_extend 门店订单状态:0新开单,1生产中,2生产完成,3已完结,5已取消
                    Integer productionStatus = orderInfoEntity.getProductionStatus();
                    if (productionStatus.intValue() == 0){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.NEW_ORDER.toInt());
                    }else if (productionStatus.intValue() == 1){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.IN_PRODUCTION.toInt());
                    }else if (productionStatus.intValue() == 2){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.PRODUCTION_FINISH.toInt());
                    }else if (productionStatus.intValue() == 3){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.FINISH.toInt());
                    }else if (productionStatus.intValue() == 5){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.CANCELLED.toInt());
                    }
                }else {
                    // 紫云网生产状态 -2.取消订单。-1:驳回，0：待分配，1.已分配，2.生产中，3.生产完成
                    String orderProductionStatus = orderInfoEntity.getOrderProductionStatus();
                    if (orderApprovalStatus.intValue() == 0){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.UN_AUDITED.toInt());
                    }else if ("0".equals(orderProductionStatus) && orderApprovalStatus.intValue() == 1){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.UN_ALLOT.toInt());
                    }else if (("1".equals(orderProductionStatus) || "2".equals(orderProductionStatus)) && orderApprovalStatus.intValue() == 1){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.IN_PRODUCTION.toInt());
                    }else if ("3".equals(orderProductionStatus)){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.PRODUCTION_FINISH.toInt());
                    }
                }
            }
            erpOrderEntity.setShipStatus(Integer.parseInt(orderInfoEntity.getShipStatus()));
            erpOrderEntity.setReceivedStatus(orderInfoEntity.getReceivedStatus()?1:0);
            erpOrderEntity.setSource(orderInfoEntity.getSource());
            erpOrderEntity.setTotalAmount(orderInfoEntity.getTotalAmount());
            erpOrderEntity.setCostFreight(orderInfoEntity.getCostFreight());
            erpOrderEntity.setDiscount(orderInfoEntity.getDiscount());
            erpOrderEntity.setPmtGoods(orderInfoEntity.getPmtGoods());
            erpOrderEntity.setPmtOrder(orderInfoEntity.getPmtOrder());
            erpOrderEntity.setPmtRandom(orderInfoEntity.getPmtRandom());
            erpOrderEntity.setPayableAmount(orderInfoEntity.getPayableAmount());
            erpOrderEntity.setPayed(orderInfoEntity.getPayed());
            erpOrderEntity.setOrderCreatetime(orderInfoEntity.getCreatetime());
            erpOrderEntity.setReceivedTime(orderInfoEntity.getReceivedTime());
            // 支付信息
            erpOrderEntity.setPayment(orderInfoEntity.getPayment());
            erpOrderEntity.setPaymentTime(orderInfoEntity.getPaymentTime());
            erpOrderEntity.setPaymentId(orderInfoEntity.getPaymentId());
            // 订单类型
            if("paper".equals(orderInfoEntity.getPromotionType())){
                erpOrderEntity.setOrderType(OrderTypeEnum.PAPER.toInt());
            } else {
                erpOrderEntity.setOrderType(OrderTypeEnum.GENERAL.toInt());
            }
            erpOrderEntity.setWeight(orderInfoEntity.getWeight());
            erpOrderEntity.setIp(orderInfoEntity.getIp());
            // 送货方式
            if(orderInfoEntity.getPickUp()){
                erpOrderEntity.setSendType(SendTypeEnum.PICK_UP.toInt());
            } else {
                erpOrderEntity.setSendType(SendTypeEnum.SEND.toInt());
            }
            erpOrderEntity.setShipName(orderInfoEntity.getShipName());
            erpOrderEntity.setShipAreaId(orderInfoEntity.getShipAreaId());
            erpOrderEntity.setShipArea(orderInfoEntity.getShipArea());
            erpOrderEntity.setShipAddr(orderInfoEntity.getShipAddr());
            erpOrderEntity.setShipZip(orderInfoEntity.getShipZip());
            erpOrderEntity.setShipTel(orderInfoEntity.getShipTel());
            erpOrderEntity.setShipTime(orderInfoEntity.getShipTime());
            erpOrderEntity.setShipMobile(orderInfoEntity.getShipMobile());
            erpOrderEntity.setReferralsCode(orderInfoEntity.getReferralsCode());
            erpOrderEntity.setMemo(orderInfoEntity.getMemo());
            // 原订单关联表信息
            erpOrderEntity.setAuditName(orderInfoEntity.getAuditName());
            erpOrderEntity.setAuditTime(orderInfoEntity.getAuditTime());
            erpOrderEntity.setAllotTime(orderInfoEntity.getSuppTime());
            erpOrderEntity.setFinishTime(orderInfoEntity.getFinishTime());
            erpOrderEntity.setOrderTag(orderInfoEntity.getOrderTag());
            erpOrderEntity.setCsId(orderInfoEntity.getCsId());
            erpOrderEntity.setPmcId(orderInfoEntity.getPmcId());
            erpOrderEntity.setSendTime(orderInfoEntity.getSendTime());
            erpOrderEntity.setDeliveryTime(orderInfoEntity.getDeliveryTime());
            erpOrderEntity.setReceiveMoney(orderInfoEntity.getReceiveMoney());
            erpOrderEntity.setReceiveMoneyDate(orderInfoEntity.getReceiveMoneyDate());
            erpOrderEntity.setReceiveMoneyRemark(orderInfoEntity.getReceiveMoneyRemark());
            erpOrderEntity.setAcceptOrderUser(orderInfoEntity.getAcceptOrderUser());
            erpOrderEntity.setProductionLiable(orderInfoEntity.getProductionLiable());
            erpOrderEntity.setProductionRequest(orderInfoEntity.getProductionRequest());
            erpOrderEntity.setMerchantsSalesMan(orderInfoEntity.getMerchantsSalesMan());
            erpOrderEntity.setSendTax(orderInfoEntity.getSendTax());
            erpOrderEntity.setIsUrgent(orderInfoEntity.getIsUrgent());
            erpOrderEntity.setCostRecord(orderInfoEntity.getCostRecord());
            erpOrderEntity.setIsReplaceSend(orderInfoEntity.getIsReplaceSend());
            erpOrderEntity.setAllotThirdStatus(orderInfoEntity.getSuppStatus());
            erpOrderEntity.setGraphicShopPayStatus(orderInfoEntity.getGraphicShopPayStatus());
            erpOrderEntity.setGraphicShopPayDate(orderInfoEntity.getGraphicShopPayDate());
            erpOrderEntity.setGraphicShopPayRemarks(orderInfoEntity.getGraphicShopPayRemarks());
            erpOrderEntity.setShareStatus(Integer.valueOf(orderInfoEntity.getShareStatus()));
            // 是否计算积分，0、未计算，1、已计算
            Boolean reckonScore = orderInfoEntity.getReckonScore();
            if (reckonScore){
                erpOrderEntity.setReckonScore(1);
            }else {
                erpOrderEntity.setReckonScore(0);
            }
            erpOrderEntity.setLastModifiedTime(orderInfoEntity.getLastModified());
            erpOrderEntity.setCreateTime(orderInfoEntity.getCreatetime());
            // 是否挂账
            if (StringUtils.equals(orderInfoEntity.getPayStatus(), "9")){
                erpOrderEntity.setPayStatus(0);
                erpOrderEntity.setOnCredit(1);
            }else {
                erpOrderEntity.setPayStatus(Integer.parseInt(orderInfoEntity.getPayStatus()));
                erpOrderEntity.setOnCredit(0);
            }
            erpOrderEntity.setYonyouOrderNo(orderInfoEntity.getYonyouOrderNo());
            // 是否收取押金
            if (StringUtils.equals(orderInfoEntity.getPayStatus(), "3")){
                erpOrderEntity.setDeposit(1);
            }else {
                erpOrderEntity.setDeposit(0);
            }
            erpOrderlist.add(erpOrderEntity);
        }



        new Thread(() -> erpOrderService.insertBatch(erpOrderlist, 2000)).start();

        long end = System.currentTimeMillis();
        long time = (end-begin)/1000/60;
        return R.ok().put("time", time);
    }

    @Override
    public R orderItemDataTransfer(String beginTime, String endTime) {
        long begin = System.currentTimeMillis();
        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(beginTime)){
            param.put("beginTime", beginTime);
        }
        if (StringUtils.isNotBlank(endTime)){
            param.put("endTime", endTime);
        }
        List<ErpOrderItemEntity> list = orderItemExtendService.selectOrderItemList(param);

//        new Thread(() -> erpOrderItemService.insertBatch(list)).start();

        long end = System.currentTimeMillis();
        long time = (end-begin)/1000/60;
        return R.ok().put("time", time);
    }

    @Override
    public R orderItemSendPicDataTransfer() {
        long begin = System.currentTimeMillis();
        String sendPics = orderItemExtendService.selectSendPicsList();
        String[] split = sendPics.split(",");
        List<OtherAccessoryDataEntity> OtherAccessoryList = orderItemExtendService.selectImageInfoList(split);

        new Thread(() -> otherAccessoryDataService.insertBatch(OtherAccessoryList, 20000)).start();

        long end = System.currentTimeMillis();
        long time = (end-begin)/1000/60;
        return R.ok().put("time", time);
    }

    @Override
    public R orderCostDataTransfer() {
        long begin = System.currentTimeMillis();

        long end = System.currentTimeMillis();
        long time = (begin-end)/1000/60;
        return R.ok().put("time", time);
    }

    @Override
    public R orderLogisticsDataTransfer() {
        long begin = System.currentTimeMillis();

        long end = System.currentTimeMillis();
        long time = (begin-end)/1000/60;
        return R.ok().put("time", time);
    }

    @Override
    public R saveOrderInfoDataTransfer(List<OrderInfoVo> orderInfoVoList) {
        List<ErpOrderEntity> erpOrderlist = new ArrayList<>();
        for (OrderInfoVo orderInfoEntity : orderInfoVoList){
            ErpOrderEntity erpOrderEntity = new ErpOrderEntity();
            erpOrderEntity.setOrderNo(orderInfoEntity.getOrderId());
            erpOrderEntity.setMemberId(orderInfoEntity.getMemberId());
            erpOrderEntity.setMemberName(orderInfoEntity.getMemberName());
            if ("erp".equals(orderInfoEntity.getSource())){
                erpOrderEntity.setOrderSource(OrderSourceEnum.OFFlINE.toInt());
            }else {
                erpOrderEntity.setOrderSource(OrderSourceEnum.ZIYUN.toInt());
             }
            if (orderInfoEntity.getSupplierId() == null){ // 工厂订单
                erpOrderEntity.setThirdMerchantType(ThirdMerchantTypeEnum.FACTOEY.toInt());
                erpOrderEntity.setThirdMerchantId(orderInfoEntity.getFactoryId());
            } else {
                if(orderInfoEntity.getSupplierId().intValue() == 1){ // 紫云自营
                    erpOrderEntity.setThirdMerchantType(ThirdMerchantTypeEnum.ZIYUN.toInt());
                } else {
                    if(orderInfoEntity.getIsSupplier().intValue() == 1){
                        erpOrderEntity.setThirdMerchantType(ThirdMerchantTypeEnum.ZIYUN_MERCHANT.toInt());
                    } else {
                        erpOrderEntity.setThirdMerchantType(ThirdMerchantTypeEnum.MERCHANT.toInt());
                    }
                }
                erpOrderEntity.setThirdMerchantId(orderInfoEntity.getSupplierId());
            }
            erpOrderEntity.setOriginalOrderId(Long.valueOf(orderInfoEntity.getOrderId()));
            // 付款状态:0:未支付,1:已支付,2:已付款至到担保方,3:部分付款,4:部分退款,5:全额退款,6:待审核订单,7:支付待审核,8:已支付待审核(转账),9:挂账(门店）
            String payStatus = orderInfoEntity.getPayStatus();
            // 原审核状态 0：未审核 1：已审核
            Integer orderApprovalStatus = orderInfoEntity.getOrderApprovalStatus();

            if("finish".equals(orderInfoEntity.getStatus())){
                erpOrderEntity.setProductionStatus(ProductionStatusEnum.FINISH.toInt());
            }else if("dead".equals(orderInfoEntity.getStatus())){
                erpOrderEntity.setProductionStatus(ProductionStatusEnum.CANCELLED.toInt());
            }else{
                // 现订单生产状态  0：待分配，1.已分配，2.生产中，3.生产完成
                // 现 erp_order生产状态  1.待审核;2.待分配;3.生产中;4.生产完成;5.已发货;6.已完成;7.新开单(门店);11.已作废
                if (StringUtils.equals(orderInfoEntity.getSource(), "erp")){
                    // 原order_info_extend 门店订单状态:0新开单,1生产中,2生产完成,3已完结,5已取消
                    Integer productionStatus = orderInfoEntity.getProductionStatus();
                    if (productionStatus.intValue() == 0){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.NEW_ORDER.toInt());
                    }else if (productionStatus.intValue() == 1){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.IN_PRODUCTION.toInt());
                    }else if (productionStatus.intValue() == 2){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.PRODUCTION_FINISH.toInt());
                    }else if (productionStatus.intValue() == 3){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.FINISH.toInt());
                    }else if (productionStatus.intValue() == 5){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.CANCELLED.toInt());
                    }
                }else {
                    // 紫云网生产状态 -2.取消订单。-1:驳回，0：待分配，1.已分配，2.生产中，3.生产完成
                    String orderProductionStatus = orderInfoEntity.getOrderProductionStatus();
                    if (orderApprovalStatus.intValue() == 0){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.UN_AUDITED.toInt());
                    }else if ("0".equals(orderProductionStatus) && orderApprovalStatus.intValue() == 1){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.UN_ALLOT.toInt());
                    }else if (("1".equals(orderProductionStatus) || "2".equals(orderProductionStatus)) && orderApprovalStatus.intValue() == 1){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.IN_PRODUCTION.toInt());
                    }else if ("3".equals(orderProductionStatus)){
                        erpOrderEntity.setProductionStatus(ProductionStatusEnum.PRODUCTION_FINISH.toInt());
                    }
                }
            }
            erpOrderEntity.setShipStatus(Integer.parseInt(orderInfoEntity.getShipStatus()));
            erpOrderEntity.setReceivedStatus(orderInfoEntity.getReceivedStatus()?1:0);
            erpOrderEntity.setSource(orderInfoEntity.getSource());
            erpOrderEntity.setTotalAmount(orderInfoEntity.getTotalAmount());
            erpOrderEntity.setCostFreight(orderInfoEntity.getCostFreight());
            erpOrderEntity.setDiscount(orderInfoEntity.getDiscount());
            erpOrderEntity.setPmtGoods(orderInfoEntity.getPmtGoods());
            erpOrderEntity.setPmtOrder(orderInfoEntity.getPmtOrder());
            erpOrderEntity.setPmtRandom(orderInfoEntity.getPmtRandom());
            erpOrderEntity.setPayableAmount(orderInfoEntity.getPayableAmount());
            erpOrderEntity.setPayed(orderInfoEntity.getPayed());
            erpOrderEntity.setOrderCreatetime(orderInfoEntity.getCreatetime());
            erpOrderEntity.setReceivedTime(orderInfoEntity.getReceivedTime());
            // 支付信息
            erpOrderEntity.setPayment(orderInfoEntity.getPayment());
            erpOrderEntity.setPaymentTime(orderInfoEntity.getPaymentTime());
            erpOrderEntity.setPaymentId(orderInfoEntity.getPaymentId());
            // 订单类型
            if("paper".equals(orderInfoEntity.getPromotionType())){
                erpOrderEntity.setOrderType(OrderTypeEnum.PAPER.toInt());
            } else {
                erpOrderEntity.setOrderType(OrderTypeEnum.GENERAL.toInt());
            }
            erpOrderEntity.setWeight(orderInfoEntity.getWeight());
            erpOrderEntity.setIp(orderInfoEntity.getIp());
            // 送货方式
            if(orderInfoEntity.getPickUp()){
                erpOrderEntity.setSendType(SendTypeEnum.PICK_UP.toInt());
            } else {
                erpOrderEntity.setSendType(SendTypeEnum.SEND.toInt());
            }
            erpOrderEntity.setShipName(orderInfoEntity.getShipName());
            erpOrderEntity.setShipAreaId(orderInfoEntity.getShipAreaId());
            erpOrderEntity.setShipArea(orderInfoEntity.getShipArea());
            erpOrderEntity.setShipAddr(orderInfoEntity.getShipAddr());
            erpOrderEntity.setShipZip(orderInfoEntity.getShipZip());
            erpOrderEntity.setShipTel(orderInfoEntity.getShipTel());
            erpOrderEntity.setShipTime(orderInfoEntity.getShipTime());
            erpOrderEntity.setShipMobile(orderInfoEntity.getShipMobile());
            erpOrderEntity.setReferralsCode(orderInfoEntity.getReferralsCode());
            erpOrderEntity.setMemo(orderInfoEntity.getMemo());
            // 原订单关联表信息
            erpOrderEntity.setAuditName(orderInfoEntity.getAuditName());
            erpOrderEntity.setAuditTime(orderInfoEntity.getAuditTime());
            erpOrderEntity.setAllotTime(orderInfoEntity.getSuppTime());
            erpOrderEntity.setFinishTime(orderInfoEntity.getFinishTime());
            erpOrderEntity.setOrderTag(orderInfoEntity.getOrderTag());
            erpOrderEntity.setCsId(orderInfoEntity.getCsId());
            erpOrderEntity.setPmcId(orderInfoEntity.getPmcId());
            erpOrderEntity.setSendTime(orderInfoEntity.getSendTime());
            erpOrderEntity.setDeliveryTime(orderInfoEntity.getDeliveryTime());
            erpOrderEntity.setReceiveMoney(orderInfoEntity.getReceiveMoney());
            erpOrderEntity.setReceiveMoneyDate(orderInfoEntity.getReceiveMoneyDate());
            erpOrderEntity.setReceiveMoneyRemark(orderInfoEntity.getReceiveMoneyRemark());
            erpOrderEntity.setAcceptOrderUser(orderInfoEntity.getAcceptOrderUser());
            erpOrderEntity.setProductionLiable(orderInfoEntity.getProductionLiable());
            erpOrderEntity.setProductionRequest(orderInfoEntity.getProductionRequest());
            erpOrderEntity.setMerchantsSalesMan(orderInfoEntity.getMerchantsSalesMan());
            erpOrderEntity.setSendTax(orderInfoEntity.getSendTax());
            erpOrderEntity.setIsUrgent(orderInfoEntity.getIsUrgent());
            erpOrderEntity.setCostRecord(orderInfoEntity.getCostRecord());
            erpOrderEntity.setIsReplaceSend(orderInfoEntity.getIsReplaceSend());
            erpOrderEntity.setAllotThirdStatus(orderInfoEntity.getSuppStatus());
            erpOrderEntity.setGraphicShopPayStatus(orderInfoEntity.getGraphicShopPayStatus());
            erpOrderEntity.setGraphicShopPayDate(orderInfoEntity.getGraphicShopPayDate());
            erpOrderEntity.setGraphicShopPayRemarks(orderInfoEntity.getGraphicShopPayRemarks());
            erpOrderEntity.setShareStatus(Integer.valueOf(orderInfoEntity.getShareStatus()));
            // 是否计算积分，0、未计算，1、已计算
            Boolean reckonScore = orderInfoEntity.getReckonScore();
            if (reckonScore){
                erpOrderEntity.setReckonScore(1);
            }else {
                erpOrderEntity.setReckonScore(0);
            }
            erpOrderEntity.setLastModifiedTime(orderInfoEntity.getLastModified());
            erpOrderEntity.setCreateTime(orderInfoEntity.getCreatetime());
            // 是否挂账
            if (StringUtils.equals(orderInfoEntity.getPayStatus(), "9")){
                erpOrderEntity.setPayStatus(0);
                erpOrderEntity.setOnCredit(1);
            }else {
                erpOrderEntity.setPayStatus(Integer.parseInt(orderInfoEntity.getPayStatus()));
                erpOrderEntity.setOnCredit(0);
            }
            erpOrderEntity.setYonyouOrderNo(orderInfoEntity.getYonyouOrderNo());
            // 是否收取押金
            if (StringUtils.equals(orderInfoEntity.getPayStatus(), "3")){
                erpOrderEntity.setDeposit(1);
            }else {
                erpOrderEntity.setDeposit(0);
            }

//            ErpOrderEntity erpOrder = erpOrderService.selectById(erpOrderEntity.getOrderNo());
//            if (erpOrder == null){
//                continue;
//            }
            erpOrderlist.add(erpOrderEntity);
        }

        if (erpOrderlist.size() != 0){
            erpOrderService.insertBatch(erpOrderlist, 2000);
        }

//        new Thread(() -> erpOrderService.insertBatch(erpOrderlist, 2000)).start();

        return R.ok();
    }

    @Override
    public void saveOrderItemDataTransfer(List<ErpOrderItemEntity> list) {

    }

}
